//
//  PurchaseGoodQiuCell.m
//  danpin
//
//  Created by chuangjia on 26/8/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "GoodQiuCell.h"

@interface GoodQiuCell()
{
    
}
@property (nonatomic, strong) UIView *bgView;
@property(nonatomic,strong)RTLabel *skuNameLabel;
@property(nonatomic,strong)RTLabel *cateLabel;
@property(nonatomic,strong)UIImageView *lineView;
@property(nonatomic,strong)UILabel *userLable;
@property(nonatomic,strong)UILabel *timeLable;
@end


@implementation GoodQiuCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        _bgView = [[UIView alloc] initWithFrame:CGRectZero];
        _bgView.backgroundColor =[UIColor clearColor];
        [self.contentView addSubview:_bgView];
        //
        _skuNameLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        _skuNameLabel.backgroundColor = [UIColor clearColor];
        _skuNameLabel.textColor = [UIColor blackColor];
        _skuNameLabel.textAlignment = NSTextAlignmentLeft;
        _skuNameLabel.font = FONT(14);
        _skuNameLabel.lineSpacing = LSpacing;
        
        [_bgView addSubview:_skuNameLabel];
        
        //
        _cateLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        _cateLabel.backgroundColor = [UIColor clearColor];
        _cateLabel.textColor = UIColorFromRGB(0x999999);
        _cateLabel.textAlignment = NSTextAlignmentLeft;
        _cateLabel.font = FONT(13);
        _cateLabel.lineSpacing =LSpacing;
        [_bgView addSubview:_cateLabel];
        
        UITapGestureRecognizer* singleRecognizer1= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFromUser)];
        _userLable =[[UILabel alloc] initWithFrame:CGRectZero];
        _userLable.backgroundColor = [UIColor clearColor];
        _userLable.textColor = UIColorFromRGB(0x285790);
        _userLable.textAlignment = NSTextAlignmentRight;
        _userLable.font = FONT(13);
        _userLable.numberOfLines=0;
        _userLable.userInteractionEnabled=YES;
        [_userLable addGestureRecognizer:singleRecognizer1];
        [_bgView addSubview:_userLable];
        singleRecognizer1=nil;
        
        _timeLable =[[UILabel alloc] initWithFrame:CGRectZero];
        _timeLable.backgroundColor = [UIColor clearColor];
        _timeLable.textColor = [UIColor grayColor];
        _timeLable.textAlignment = NSTextAlignmentRight;
        _timeLable.font = FONT(13);
        _timeLable.numberOfLines=0;
        [_bgView addSubview:_timeLable];
        
        _lineView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _lineView.image =[UIImage imageNamed:@"line_xu.png"];
        [_bgView addSubview:_lineView];
    }
    return self;
}
-(void)dealloc
{
    self.actionBlock=nil;
    self.data=nil;
    self.indexPath=nil;
    self.key=nil;
    self.other=nil;
    
    [self.skuNameLabel removeFromSuperview];
    self.skuNameLabel=nil;
    [self.cateLabel removeFromSuperview];
    self.cateLabel =nil;
    self.lineView.image=nil;
    [self.lineView removeFromSuperview];
    self.lineView=nil;
    [self.userLable removeFromSuperview];
    self.userLable =nil;
    [self.timeLable removeFromSuperview];
    self.timeLable=nil;
    [self.bgView removeFromSuperview];
    self.bgView=nil;
    
    
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _bgView.frame =CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    
}
- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    if ([self.other integerValue]==1) {
         self.lineView.frame =CGRectMake(0,self.bounds.size.height-1, DEVICE_WIDTH, 1);
    }
   
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model other:(id)other key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.other =other;
    self.key =key;
    self.data =model;
    [self showInfo:model indexPath:indexPath];
}
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.key = key;
    self.data =model;
    [self showInfo:model indexPath:indexPath];
}
- (void)showInfo:(id)model indexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict =(NSDictionary*)model;
    self.data =model;
    self.indexPath=indexPath;
    float fTop = 0;
    fTop += PHOTO_FRAME_WIDTH;
    
    //
    NSString *skuname =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"title"]] replaceNullString];
    if (skuname.length>0) {
        [_skuNameLabel setText:[NSString stringWithFormat:@"%@",skuname]];
        CGSize optimumSize = [_skuNameLabel optimumSize];
        CGFloat hh =optimumSize.height;
        if (hh<20) {
            hh=20;
        }
        _skuNameLabel.frame = CGRectMake(PHOTO_FRAME_WIDTH,fTop, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, hh);
        
        fTop = fTop+hh+PHOTO_FRAME_WIDTH;
    }
    
    NSString *ruser =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"recommend_count"]] replaceNullString];
    if (ruser.length==0 || [ruser isEqualToString:@"(null)"])
    {
        ruser =@"0";
    }
    NSString *puser =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"recommend_user"]] replaceNullString];
    if (puser.length==0 || [puser isEqualToString:@"(null)"])
    {
        puser =@"0";
    }
    
    NSString *recommend_user =[NSString stringWithFormat:@"%@个推荐 / %@个人参与", ruser,puser];
    _cateLabel.frame =CGRectMake(PHOTO_FRAME_WIDTH,fTop,DEVICE_WIDTH -PHOTO_FRAME_WIDTH*2, PHOTO_FRAME_WIDTH*2);
    _cateLabel.text =recommend_user;
    //
    NSString *upload_dateinfo = [[NSString stringWithFormat:@"%@",[dict objectForKey:@"upload_dateinfo"]] replaceNullString];
    NSDictionary *attribute = @{NSFontAttributeName:FONT(14)};
    CGSize retSize = [upload_dateinfo boundingRectWithSize:CGSizeMake(DEVICE_WIDTH/2, 0)
                                                   options:
                      NSStringDrawingTruncatesLastVisibleLine |
                      NSStringDrawingUsesLineFragmentOrigin |
                      NSStringDrawingUsesFontLeading
                                                attributes:attribute
                                                   context:nil].size;
    _timeLable.frame =CGRectMake(DEVICE_WIDTH-retSize.width-PHOTO_FRAME_WIDTH, fTop, retSize.width, PHOTO_FRAME_WIDTH*2);
    
    
    _timeLable.text =upload_dateinfo;
    
    NSString *nick = [[NSString stringWithFormat:@"%@",[[dict objectForKey:@"user"] objectForKey:@"nick_name"]] replaceNullString];
    NSDictionary *attribute1 = @{NSFontAttributeName:FONT(14)};
    CGSize retSize1 = [nick boundingRectWithSize:CGSizeMake(DEVICE_WIDTH/2, 0)
                                         options:
                       NSStringDrawingTruncatesLastVisibleLine |
                       NSStringDrawingUsesLineFragmentOrigin |
                       NSStringDrawingUsesFontLeading
                                      attributes:attribute1
                                         context:nil].size;
    _userLable.frame =CGRectMake(_timeLable.frame.origin.x -retSize1.width-PHOTO_FRAME_WIDTH , fTop, retSize1.width, PHOTO_FRAME_WIDTH*2);
    _userLable.text =nick;
    [self layoutSubviews];
}


/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    NSDictionary *dict =(NSDictionary*)model;
    NSString *skuname =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"title"]] replaceNullString];
   
    CGFloat hight =PHOTO_FRAME_WIDTH;
    if (skuname.length>0) {
        RTLabel *text1Label =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        text1Label.font =FONT(14);
        text1Label.lineSpacing = LSpacing;
        [text1Label setText:[NSString stringWithFormat:@"%@",skuname]];
        CGSize optimumSize = [text1Label optimumSize];
        text1Label=nil;
        CGFloat hh1 =optimumSize.height;
        
        hight = hight +hh1+PHOTO_FRAME_WIDTH;
    }
    return hight+PHOTO_FRAME_WIDTH*3;
    
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)bangdanAction
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_recommendTobangdan,self,self.data,self.key,self.indexPath);
    }
}
-(void)handleSingleTapFromUser
{
    NSDictionary *dicdata =[self.data objectForKey:@"user"];
    if (self.actionBlock) {
        self.actionBlock(BEventType_sunUser,self,dicdata,self.key,self.indexPath);
    }
}
@end
